package lumingweihua.future.cn.lumingweihua.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;


import butterknife.BindView;
import butterknife.ButterKnife;
import lumingweihua.future.cn.lumingweihua.R;

/**
 * Author       zhusx
 * Email        327270607@qq.com
 * Created      2018/3/15 13:22
 */

public class TitleBarView extends LinearLayout {
    @BindView(R.id.tv_titleBar_title)
    TextView titleTV;
    @BindView(R.id.tv_titleBar_back)
    TextView backTV;
    @BindView(R.id.tv_titleBar_right)
    public TextView rightTV;

    public TitleBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public TitleBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_titlebar, this, true);
        ButterKnife.bind(this);
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TitleBarView);
        String st = typedArray.getString(R.styleable.TitleBarView_title);
        if (!TextUtils.isEmpty(st)) {
            titleTV.setText(st);
        }
        String backText = typedArray.getString(R.styleable.TitleBarView_leftText);
        if (backText != null) {
            backTV.setText(backText);
        }
        boolean isVi = typedArray.getBoolean(R.styleable.TitleBarView_backVisibility, true);
        if (!isVi) {
            backTV.setVisibility(INVISIBLE);
            backTV.setOnClickListener(null);
        }
        int resRightImg = typedArray.getResourceId(R.styleable.TitleBarView_rightResource, -1);
        if (resRightImg != -1) {
            Drawable d = getResources().getDrawable(resRightImg);
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            rightTV.setCompoundDrawables(null, null, d, null);
        }
        String rightText = typedArray.getString(R.styleable.TitleBarView_rightText);
        if (rightText != null) {
            rightTV.setText(rightText);
        }
        typedArray.recycle();
    }
}
